package com.example.whm;

import java.util.HashMap;
import java.util.Map;

import org.apache.http.cookie.Cookie;
import org.holoeverywhere.app.Activity;
import org.holoeverywhere.app.Application;
import org.holoeverywhere.preference.SharedPreferences;
import org.holoeverywhere.widget.Toast;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import com.example.whm.lib.client;
import com.example.whm.model.User;

public class Login extends Activity  {
	AQuery aq;
	String id;
	 AQuery aq2;
	 TextView mTv;
	 int width;
	 Bundle b;
	SharedPreferences sp;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//requestWindowFeature(Window.FEATURE_NO_TITLE); 
		//getSupportActionBar().hide();		
		setContentView(R.layout.login);
		
		// webview=(WebView) findViewById(R.id.entity_detail_web);
		//webview.getSettings().setBuiltInZoomControls(false);
		sp=((Application)getApplication()).getSharedPreferences("whm",Context.MODE_PRIVATE);
		aq=new AQuery(this);
		aq.id(R.id.login_btn).clicked(this, "login_func"); 
		aq.id(R.id.login_register).clicked(this, "register_func"); 
	
	    b=getIntent().getExtras();
	   
		/* LinearLayout line=(LinearLayout) findViewById(R.id.entity_detail_line);
		 mTv= (TextView) findViewById(R.id.entity_detail_cont); 
		 aq2=new AQuery(this,line);
		 Bundle b= getIntent().getExtras();
		 if (b!=null) {
			 Log.i("EntityDetail", "getIntent().getExtras()  id="+b.getString("id"));
			 id=b.getString("id");
			 init(id);
		}*/
	
	
		 
	}
	
	public void register_func(){
		startActivity(new Intent(Login.this, Register.class));
		finish();
	}
	public void login_func(){
		System.out.println("login_func");
		String username=aq.id(R.id.login_username).getText().toString();
		String password=aq.id(R.id.login_password).getText().toString();
		if (!username.trim().equals("")&&!password.trim().equals("")) {
			String url ="http://"+Whm_app.api_url+":9000/api/login";
	        
		    Map<String, Object> params = new HashMap<String, Object>();
		    params.put("name", username);
		    params.put("passwd", password);
		        
		    aq.ajax(url, params, String.class, new AjaxCallback<String>() {

		        @Override
		        public void callback(String url, String s, AjaxStatus status) {
		         
		        	String state= JSON.parseObject(s).getString("result");
		        	if (state.equals("ok")) {		        	
		        		client.pre_cache_put(sp, "user",JSON.parseObject(s).getString("user"));
					}
		        	//user_websocket(JSON.parseObject(s).getString("user")); 
		        	Toast.makeText(Login.this, "callback ", 1000).show();
		            for(Cookie c:status.getCookies()){
		            	String name=c.getName();
		            	String value=c.getValue();
		            	 
		            	Toast.makeText(Login.this, "getCookies ", 1000).show();
		            	
		            	System.out.println("cookie c.getName()==="+name);
		            	System.out.println("cookie c.getValue()==="+value);
		            	if (name!=null&&value!=null) {
		            		
		                	
							client.pre_cache_put(sp, "cookie_name",name);
							client.pre_cache_put(sp, "cookie_value",value);
							/*if (b.getString("skip")==null) {
								
								Intent i=new Intent(Login.this, MainActivity.class);
								i.putExtras(b);
								startActivity(i);
							}*/
							
						
							
						}
		            	
		            	
		            }
		        	Login.this.finish();
		            //new Intent(Login.this, cls);
		            
		        	System.out.println(status.getCode());   
		            System.out.println(s); 
		           
		        }
		    });
			
		}
	}
	
	public void user_websocket(String u){
		User user=JSONObject.parseObject(u, User.class);
		   Map<String, Object> params = new HashMap<String, Object>();
		    params.put("userid", user.getId());
		    params.put("sessionid", sp.getString("sessionid", ""));
		    String url ="http://"+Whm_app.api_url+":9000/api/user_websocket";   
		    aq.ajax(url, params, String.class, new AjaxCallback<String>() {

				@Override
				public void callback(String url, String object,
						AjaxStatus status) {
					// TODO Auto-generated method stub
					
					
					
				}
		    	
		    	
		    });
		    
	}
	

}
